package com.zhangxiaofanfan.hotcode.fourth.third;

/**
 * TODO
 *
 * @author zhangxiaofanfan
 * @date 2024-06-03 23:17:36
 */
public class HotCode042 {
    public static void main(String[] args) {
        int[] nums = {-10, -3, 0, 5, 9};
        HotCode042 hotCode = new HotCode042();
        TreeNode treeNode = hotCode.sortedArrayToBST(nums);
        System.out.println("end...");
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        return sortedArrayToBST(nums, 0, nums.length - 1);
    }

    public TreeNode sortedArrayToBST(int[] nums, int left, int right) {
        if (left > right) {
            return null;
        }
        int mid = left + ((right - left) >> 1);
        TreeNode node = new TreeNode(nums[mid]);
        node.left = sortedArrayToBST(nums, left, mid - 1);
        node.right = sortedArrayToBST(nums, mid + 1, right);
        return node;
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
